from pydantic import BaseModel, EmailStr, Field
from typing import List, Optional
import datetime

# 用户相关模式
class UserBase(BaseModel):
    name: str
    email: Optional[str] = None
    remark: Optional[str] = None

class UserCreate(UserBase):
    password: str

class UserUpdate(BaseModel):
    name: Optional[str] = None
    email: Optional[str] = None
    password: Optional[str] = None
    remark: Optional[str] = None
    menu_json: Optional[str] = None
    is_admin: Optional[int] = None
    is_active: Optional[int] = None

class UserResponse(UserBase):
    id: int
    is_admin: int
    is_active: int
    created_at: datetime.datetime
    updated_at: datetime.datetime

    class Config:
        orm_mode = True

# 菜单相关模式
class MenuBase(BaseModel):
    pid: int
    name: str
    icon: str
    code: str
    path: str
    is_active: int = 0

class MenuCreate(MenuBase):
    pass

class MenuUpdate(BaseModel):
    pid: Optional[int] = None
    name: Optional[str] = None
    icon: Optional[str] = None
    code: Optional[str] = None
    path: Optional[str] = None
    is_active: Optional[int] = None

class MenuResponse(MenuBase):
    id: int
    created_at: datetime.datetime
    updated_at: datetime.datetime

    class Config:
        orm_mode = True

# 日志相关模式
class LogBase(BaseModel):
    user_id: Optional[int] = None
    username: Optional[str] = None
    method: Optional[str] = None
    path: Optional[str] = None
    status_code: Optional[int] = None
    ip: Optional[str] = None
    user_agent: Optional[str] = None
    request_data: Optional[str] = None
    response_data: Optional[str] = None
    error_msg: Optional[str] = None
    log_type: Optional[str] = None

class LogCreate(LogBase):
    pass

class LogResponse(LogBase):
    id: int
    created_at: datetime.datetime

    class Config:
        orm_mode = True

# 文件相关模式
class FileBase(BaseModel):
    file_name: str
    file_url: str
    file_type: Optional[str] = None
    file_size: Optional[int] = None
    uploader_id: Optional[int] = None
    uploader_name: Optional[str] = None
    remark: Optional[str] = None

class FileCreate(FileBase):
    pass

class FileUpdate(BaseModel):
    file_name: Optional[str] = None
    file_url: Optional[str] = None
    file_type: Optional[str] = None
    file_size: Optional[int] = None
    uploader_id: Optional[int] = None
    uploader_name: Optional[str] = None
    remark: Optional[str] = None

class FileResponse(FileBase):
    id: int
    created_at: datetime.datetime
    updated_at: datetime.datetime

    class Config:
        orm_mode = True

# 登录相关模式
class Token(BaseModel):
    access_token: str
    token_type: str

class TokenData(BaseModel):
    username: Optional[str] = None

class LoginForm(BaseModel):
    username: str
    password: str

# 菜单树节点
class MenuTreeNode(BaseModel):
    id: int
    pid: int
    name: str
    code: str
    path: str
    icon: str
    is_active: int
    children: List["MenuTreeNode"] = []

    class Config:
        orm_mode = True

# 监控相关的schema
class SystemInfo(BaseModel):
    hostname: str
    os_name: str
    os_version: str
    uptime: str
    boot_time: str

class ResourceUsage(BaseModel):
    cpu_percent: float
    cpu_count: int
    memory_total: str
    memory_used: str
    memory_percent: float
    disk_total: str
    disk_used: str
    disk_percent: float
    
class CPUHistory(BaseModel):
    timestamp: str
    value: float
    
class MemoryHistory(BaseModel):
    timestamp: str
    value: float

class MonitorResponse(BaseModel):
    system_info: SystemInfo
    resource_usage: ResourceUsage
    cpu_history: List[CPUHistory]
    memory_history: List[MemoryHistory]

    class Config:
        orm_mode = True 